﻿

CREATE proc [dbo].[GetAccountName]
@accountNo int,
@company char(2)
as
begin
	if (coalesce(@company,'') != 'Ef' and coalesce(@company,'') != 'Og')
	begin
		select	top 1 coalesce(a.AccountName, '') 'AccountName'
		from	accounting.AccountsStructure a
				inner join accounting.mnAccount2Company a2c on
					a2c.AccountID = a.AccountID
				inner join dbo.Organisation o on
					o.OrgKey = a2c.OrgKey
		where	cast(a.AccountNumber as int) = @accountNo
				and o.Suchbegriff = @company
	end
	else
	begin
		select	top 1 coalesce(a.AccountName, '') 'AccountName'
		from	accounting.AccountsStructure a
		where	cast(a.AccountNumber as int) = @accountNo
				and (a.Ef != 0 or a.Og != 0)
	end
end



GO
GRANT EXECUTE
    ON OBJECT::[dbo].[GetAccountName] TO PUBLIC
    AS [dbo];

